{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "28e43781",
   "metadata": {},
   "source": [
    "# 字典的课本练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "77bd856e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'ford perfect', 'gender': 'male', 'occupation': 'researcer', 'home planet': 'betelgeuse seven'}\n"
     ]
    }
   ],
   "source": [
    "person3={'name':'ford perfect',\n",
    "         'gender':'male',\n",
    "        'occupation':'researcer',\n",
    "        'home planet':'betelgeuse seven'}\n",
    "print(person3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e91b7058",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'ford perfect',\n",
       " 'gender': 'male',\n",
       " 'occupation': 'researcer',\n",
       " 'home planet': 'betelgeuse seven',\n",
       " 'age': 33}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3['age']=33\n",
    "person3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8e4af6c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels:a\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels=['a','e','i','o','u']\n",
    "word=input(\"provide a word to search for vowels:\")\n",
    "found=[]\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "        print(vowel)\n",
    "found={}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "562fe008",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['a']=0\n",
    "found['e']=0\n",
    "found['i']=0\n",
    "found['o']=0\n",
    "found['u']=0\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "982052b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 1, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['e']=found['e']+1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "56a91c7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 2, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['e']+=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8a171686",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k in found:\n",
    "    print(k,'was found',found[k],'time(s).')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8207c7c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k in sorted(found):\n",
    "    print(k,'was found',found[k],'time(s).')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0f04439",
   "metadata": {},
   "source": [
    "# 避免运行时出现keyerror"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5d5c0041",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits={\n",
    "     }\n",
    "fruits['apples']=10\n",
    "fruits\n",
    "'apples' in fruits"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "458c4fed",
   "metadata": {},
   "source": [
    "# 用“in”检查成员关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ccbd7022",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 3}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'bananas'in fruits:\n",
    "    fruits['bananas']+=1\n",
    "else:\n",
    "    fruits['bananas']=1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "99aad830",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 3, 'pear': 0, 'pears': 2}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'pears' not in fruits:\n",
    "    fruits['pears']=0\n",
    "fruits['pears']+=1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "789cd452",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 3, 'pear': 0, 'pears': 3}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits.setdefault('pears',0)\n",
    "fruits['pears']+=1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c80c7ac4",
   "metadata": {},
   "source": [
    "* 修复原来error的代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bf090eba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels:i\n",
      "i was found 1 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels=['a','e','i','o','u']\n",
    "word=input(\"provide a word to search for vowels:\")\n",
    "found={}\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter,0)\n",
    "        found[letter]+=1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34f96a84",
   "metadata": {},
   "source": [
    "# 集合不允许有重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3677fd3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels={'a','e','e','i','o','u','u'}\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b4657709",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2=set('aeeiouu')\n",
    "vowels2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "095faa45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'h', 'i', 'l', 'o', 'u'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels=set('aeiou')\n",
    "word='hello'\n",
    "u=vowels.union(set(word))# “union”将一个集合与另一个集合合并，再把合并结果赋予一个新变量u\n",
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e890a89e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'h', 'i', 'l', 'o', 'u']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u_list=sorted(list(u))# 集合转化成有序列表\n",
    "u_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "8043af9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'u'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d=vowels.difference(set(word)) # difference函数将vowels中的对象与set（Word）中的对象进行比较，然后返回一个新的对象集合（d）\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "4608b830",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i=vowels.intersection(set(word))# intersection 的方法确认； 元音e和o早Word变量中\n",
    "i"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ccab823",
   "metadata": {},
   "source": [
    "# 元组的意义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "564e21ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels=['a','e','i','o','u']\n",
    "type(vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "91d89df5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2=('a','e','i','o','u')\n",
    "type(vowels2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "88ef2578",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'e', 'i', 'o', 'u']\n",
      "('a', 'e', 'i', 'o', 'u')\n"
     ]
    }
   ],
   "source": [
    "print(vowels)\n",
    "print(vowels2)# 小括号指示这是一个元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "276fcb18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'I', 'o', 'u']"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组是不可变的\n",
    "vowels[2]='I'\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "86a9c35f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'e', 'i', 'o', 'u')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels[2]='I'\n",
    "vowels2# I是没有发生变化的元组不可变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "34b0465d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'python'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 注意只有一个对象的元组\n",
    "t=('python')\n",
    "type(t)\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "2dfd0dee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('python',)"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2=('python',)\n",
    "type(t2)\n",
    "t2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe6305c4",
   "metadata": {},
   "source": [
    "# 储存一个数据结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "c2d3419b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ford': {'name': 'ford perfect',\n",
       "  'gender': 'male',\n",
       "  'occupation': 'researcer',\n",
       "  'home planet': 'betelgeuse seven'}}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 包含字典的字典\n",
    "people={}\n",
    "people['ford']={'name':'ford perfect',\n",
    "         'gender':'male',\n",
    "        'occupation':'researcer',\n",
    "        'home planet':'betelgeuse seven'}\n",
    "people"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "1874c884",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ford': {'name': 'ford perfect',\n",
       "  'gender': 'male',\n",
       "  'occupation': 'researcer',\n",
       "  'home planet': 'betelgeuse seven'},\n",
       " 'arthur': {'name': '123',\n",
       "  'gender': 'male',\n",
       "  'occupation': 'maker',\n",
       "  'home planet': 'earth'},\n",
       " 'aaa': {'name': '456',\n",
       "  'gender': 'female',\n",
       "  'occupation': 'player',\n",
       "  'home planet': 'earth'}}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 嵌套字典（也就是表）\n",
    "people['arthur']={'name':'123',\n",
    "         'gender':'male',\n",
    "        'occupation':'maker',\n",
    "        'home planet':'earth'}\n",
    "people['aaa']={'name':'456',\n",
    "         'gender':'female',\n",
    "        'occupation':'player',\n",
    "        'home planet':'earth'}\n",
    "people"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "7370d9cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'aaa': {'gender': 'female',\n",
      "         'home planet': 'earth',\n",
      "         'name': '456',\n",
      "         'occupation': 'player'},\n",
      " 'arthur': {'gender': 'male',\n",
      "            'home planet': 'earth',\n",
      "            'name': '123',\n",
      "            'occupation': 'maker'},\n",
      " 'ford': {'gender': 'male',\n",
      "          'home planet': 'betelgeuse seven',\n",
      "          'name': 'ford perfect',\n",
      "          'occupation': 'researcer'}}\n"
     ]
    }
   ],
   "source": [
    "# 美观打印的复杂的数据结构\n",
    "import pprint\n",
    "pprint.pprint(people)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "a785d6c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '456',\n",
       " 'gender': 'female',\n",
       " 'occupation': 'player',\n",
       " 'home planet': 'earth'}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 访问一个复杂的数据结构的数据\n",
    "people['aaa']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "291d4cd1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'player'"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people['aaa']['occupation']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80d8fda7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
